package mo.tags.stack;

import java.util.HashMap;
import java.util.Map;

public class L1124 {

    public int longestWPI(int[] hours) {
        int maxInterval = 0;
        Map<Integer, Integer> indices = new HashMap<>();
        int sum = 0;
        int n = hours.length;
        for (int i = 0; i < n; i++) {
            int score = hours[i] > 8 ? 1 : -1;
            sum += score;
            if (sum > 0) {
                maxInterval = Math.max(maxInterval, i + 1);
            } else if (indices.containsKey(sum - 1)) {
                int interval = i - indices.get(sum - 1);
                maxInterval = Math.max(maxInterval, interval);
            }
            indices.putIfAbsent(sum, i);
        }
        return maxInterval;
    }

    public static void main(String[] args) {
        L1124 test = new L1124();
        System.out.println(test.longestWPI(new int[]{6, 9, 9, 0, 0, 0, 0, 0, 0, 9, 9, 9}));
    }

}
